package com.weirdor.elasticsearch.plugin;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 * @author weirdor
 * 文件描述:支持更多特性的Elasticsearch
 *
 */
public class ElasticSearchPlugin {

    private static Settings.Builder settings = Settings.builder();

    private static List<TransportAddress> hostList = new ArrayList<TransportAddress>();

    private static TransportClient client;

    protected final Logger logger = Logger.getLogger(this.getClass().getName());

    public ElasticSearchPlugin add(String host) {
        try {
            ElasticSearchPlugin.hostList.add(new TransportAddress(InetAddress.getByName(host), 9300));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return this;
    }

    public ElasticSearchPlugin add(String host, int port) {
        try {
            ElasticSearchPlugin.hostList.add(new TransportAddress(InetAddress.getByName(host), port));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return this;
    }

    /**
     * 不设置群集
     * @return  this
     */
    public ElasticSearchPlugin opition() {
        ElasticSearchPlugin.settings.put(Settings.EMPTY);
        return this;
    }

    public ElasticSearchPlugin opition(String clusterName, String SecurityUser, Boolean sniff, Boolean SslEnabled) {
        ElasticSearchPlugin.settings = Settings.builder().put("cluster.name", clusterName)
                .put("xpack.security.transport.ssl.enabled", SslEnabled)
                .put("xpack.security.user", SecurityUser)
                .put("client.transport.sniff", sniff);
        return this;
    }

    /**
     * 设置集群
     *
     * @param clusterName 群集名
     * @param flag        自动嗅探配置
     * @return  this
     */
    public ElasticSearchPlugin opition(String clusterName, Boolean flag) {
        settings.put("cluster.name", clusterName).build();
        return this;
    }

    public TransportClient getTransportClient() {
        try {
            if (hostList.size() <= 0) {
                return null;
            }
            client = new PreBuiltTransportClient(settings.build());
            //添加集群IP列表
            for (int i = 0; i < hostList.size(); i++) {
                client.addTransportAddress(hostList.get(i));
            }
            logger.info("连接成功！！！！");
            Map<String, Settings> asMap = client.settings().getAsGroups();
            asMap.forEach((k, v) -> {
                logger.info(k + " = " + v);
            });
            return client;

        } catch (Exception e) {
            throw new RuntimeException("无法连接elasticSearch,请检查配置!Can't connect elasticSearch!");
        }
    }

    public void tearDown() throws Exception {
        if (client != null) {
            logger.info("关闭连接");
            client.close();
        }
    }

}
